import {
	RouterMount,
	createRouter
} from 'uni-simple-router';
import Vue from 'vue'
const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,
	routes: [...ROUTES,
		{
			path: "*",
			redirect: (to) => {
				return {
					name: '404'
				}
			}
		}
	]
});
//全局路由前置守卫
router.beforeEach((to, from, next) => {
	let isLogin = Vue.prototype.$user.isLogin();

	if (to.meta && to.meta.FItemId) {
			console.log('setFItemId',to.meta.FItemId)
		Vue.prototype.$storage.setStorageSync('FItemId', to.meta.FItemId);
		//获取到用户的权限
		let UserRights = Vue.prototype.$user.getAuth(to.meta.FItemId);
		console.log('getAuth.UserRights', UserRights)
		let auth = true;
		if (!UserRights) {
			auth = false;
		}
		if (UserRights && UserRights.meta && UserRights.meta.type == 'ADD' && UserRights.FAdd != 1) {
			console.log('to.meta.add', UserRights)
			auth = false;
		}
		if (UserRights && UserRights.meta && UserRights.meta.type == 'VIEW' && UserRights.FAudit != 1) {
			console.log('to.meta.view', UserRights)
			auth = false;
		}
		// if (!auth) {
		// 	next({
		// 		path: '/pages/index',
		// 	})
		// 	setTimeout(() => {
		// 		uni.showToast({
		// 			icon: 'none',
		// 			title: '暂无权限访问该页面,请联系管理员'
		// 		})
		// 	}, 200)
		// }
	}
	if (!isLogin && to.meta.needlogin) {
		let {
			query,
			path
		} = to;
		let str = '?';
		for (let key in query) {
			str += `${key}=${query[key]}&`
		}
		let url = path + str.slice(0, -1);
		next({
			path: '/discoveryPages/login',
			query: {
				redirect: url
			}
		})

	} else {
		next()
	}
});
// 全局路由后置守卫
router.afterEach((to, from) => {
	// console.log(to)
})

export {
	router,
	RouterMount
}